/**
 * 
 */
package com.sq.partner.manager.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sq.partner.manager.common.Constants;
import com.sq.partner.manager.common.Result;
import com.sq.partner.manager.mybatis.model.GatewayApp;
import com.sq.partner.manager.mybatis.model.GatewayAppTranstype;
import com.sq.partner.manager.mybatis.model.GatewayUser;
import com.sq.partner.manager.service.IGatewayAppService;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.StrUtil;

/**
 * @author liaokailei
 * 应用配置
 *
 */
@Controller
@RequestMapping(value = "/appManager")
public class AppManagerController{
	@Resource
	private IGatewayAppService appService;
	
	/**
	 * 进入appManage.jsp前跳转
	 * @return
	 */
	@RequestMapping("/appManage")
	public String appManage(){
		return "appManage/appConfig";
	}
	
	/**
	 * 按照appName分页查询app
	 * @param appName
	 * @param pageIndex
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/queryByAppName")
	@ResponseBody
	public Page queryByAppName(String appId,Integer pageIndex) throws Exception {
		Page page=new Page();
		page.setCurrent(pageIndex);
		
		Page p = appService.queryPage(page, appId);
		
		return p;
	}
	
	/**
	 * 根据appId查询对应的app
	 * @param appId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryAppByAppId")
	@ResponseBody
	public GatewayApp queryAppByAppId(String appId) throws Exception{
		GatewayApp app = appService.queryAppByAppId(appId);
		return app;
	}
	
	/**
	 * 添加或修改app
	 * @param flag
	 * @param appId
	 * @param appName
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/saveOrUpdateApp")
	@ResponseBody
	public Result saveOrUpdateApp(String flag,String appId,String appName) throws Exception{
		
		if(StrUtil.isEmpty(flag)){
			return Result.error("系统异常");
		}else if(StrUtil.isEmpty(appId)){
			return Result.error("网关应用代码不能为空");
		}else if(appId.length() > 20){
			return Result.error("网关应用代码长度不能超过20个字符");
		}else if(StrUtil.isEmpty(appName)){
			return Result.error("网关应用名称不能为空");
		}else if(appName.length() > 50){
			return Result.error("网关应用名称长度不能超过50个字符");
		}
		
		GatewayApp app = new GatewayApp();
		app.setAppid(appId);
		app.setAppname(appName);
		
		appService.saveOrUpdateApp(flag, app);
		
		if(flag.equals("update")){
			return Result.success("修改成功");
		}else if(flag.equals("save")){
			return Result.success("添加成功");
		}
		return null;
	}
	
	/**
	 * 删除app
	 * @param appId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/delApp")
	@ResponseBody
	public Result delApp(String appId) throws Exception{
		if(StrUtil.isEmpty(appId)){
			return  Result.error("网关应用代码不能为空");
		}
		Result r = appService.delApp(appId);
		return  r;
	}
	
	/**
	 * 查找所有app(List<GatewayApp>)
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryAllAppList")
	@ResponseBody
	public List<GatewayApp> queryAllAppList() throws Exception{
		return appService.queryAllAppList();
	}
	
	/**
	 * 查找权限下所有app 
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 * 
	 */
	@RequestMapping("/queryList")
	@ResponseBody
    public List<GatewayApp> queryList(HttpSession session) throws Exception {
		GatewayUser gatewayuser=(GatewayUser) session.getAttribute(Constants.SESSION_USER);
		GatewayApp gatewayApp=new GatewayApp();
		gatewayApp.setDepts(gatewayuser.getDepts());
		return appService.queryList(gatewayApp);
    }
	
	/**
	 * 检查传入的transCode在传入的app中是否存在
	 * @param appId
	 * @param transCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/checkTransCodeIfUsedInOneApp")
	@ResponseBody
	public GatewayAppTranstype checkTransCodeIfUsedInOneApp(String appId,String transCode) throws Exception{
		
		GatewayAppTranstype appTrnstype = appService.checkTransCodeIfUsedInOneApp(appId, transCode);
		return appTrnstype;
	}
	
	/**
	 * 根据appId查询与之关联的所有appTranstype
	 * @param appId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryAppTranstypeByFkAppId")
	@ResponseBody
	public List<GatewayAppTranstype> queryAppTranstypeByFkAppId (String appId)throws Exception{
		GatewayAppTranstype appTranstype = new GatewayAppTranstype();
		appTranstype.setFkAppId(appId);
		List<GatewayAppTranstype> appTranstypeList = appService.queryAppTranstypeByFkAppId(appTranstype);
		return appTranstypeList;
	}
	
	/**
	 * 根据appTranstypeId删除对应的appTranstype
	 * @param transtypeId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("delAppTranstypeByAppTranstypeId")
	@ResponseBody
	public Result delAppTranstypeByAppTranstypeId(Long appTranstypeId) throws Exception{
		Result result = appService.delAppTranstypeByAppTranstypeId(appTranstypeId);
		return result;
	}
	
	/**
	 * 添加appTranstype
	 * @param transCode
	 * @param transName
	 * @param fkAppId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/saveAppTranstype")
	@ResponseBody
	public Result saveAppTranstype(String transCode,String transName,String fkAppId) throws Exception{
		

		if(StrUtil.isEmpty(fkAppId)){
			return Result.error("网关应用代码不能为空");
		}else if(fkAppId.length() > 20){
			return Result.error("网关应用代码长度不能超过20个字符");
		}else if(StrUtil.isEmpty(transCode)){
			return Result.error("交易代码不能为空");
		}else if(transCode.length() > 20){
			return Result.error("应用代码长度不能超过50个字符");
		}else if(StrUtil.isEmpty(transName)){
			return Result.error("交易名称不能为空");
		}else if(transName.length() > 50){
			return Result.error("交易名称长度不能超过50个字符");
		}
		
		GatewayAppTranstype appTranstype = new GatewayAppTranstype();
		appTranstype.setTransCode(transCode);
		appTranstype.setTransName(transName);
		appTranstype.setFkAppId(fkAppId);
		appService.saveAppTranstype(appTranstype);
		return Result.success("添加成功");
	}

}
